﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BaseicModuleExpression.DBExtend
{
    public static class ExtendExpression
    {
        public static string Delete<T>(this IQueryable<T> values, Expression<Func<T, bool>> expression)
        {
            ParseExpression parseExpression = new ParseExpression();
            var xx = parseExpression.LinqToSql(expression);
            string sql = string.Format("DELETE FROM [{0}] WHERE {1};"
            , typeof(T).Name//有可能还得根据泛型去获取
            , xx);
            //ExecuteNonQuery("", sql);
            return sql;
        }

        [DynamicDependency("SkipWhile`1", typeof(Enumerable))]
        public static IQueryable<TSource> SkipWhile<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(predicate);

            return source.Provider.CreateQuery<TSource>(
                Expression.Call(
                    null,
                    new Func<IQueryable<TSource>, Expression<Func<TSource, bool>>, IQueryable<TSource>>(SkipWhile).Method,
                    source.Expression, Expression.Quote(predicate)));
        }

        public static IQueryable<T> SelectEx<T>(this IQueryable<T> values, Expression<Func<T, bool>> expression)
        {
            ArgumentNullException.ThrowIfNull(values);
            ArgumentNullException.ThrowIfNull(expression);
            var data = values.Provider.CreateQuery<T>(
                Expression.Call(
                    null,
                    new Func<IQueryable<T>, Expression<Func<T, bool>>, IQueryable<T>>(SkipWhile).Method,
                    values.Expression, Expression.Quote(expression)));
            return data;
        }

        public static string Select<T>(this IQueryable<T> values, Expression<Func<T, bool>> expression)
        {
            ParseExpression parseExpression = new ParseExpression();
            var xx = parseExpression.LinqToSql(expression);
            string sql = string.Format("Select * FROM [{0}] WHERE {1};"
            , typeof(T).Name//有可能还得根据泛型去获取
            , xx);
            //ExecuteNonQuery("", sql);
            return sql;
        }

        /// <summary>
        /// 执行T-sql 语句
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private static int ExecuteNonQuery(string connectionString, string sql)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = sql;

                if (conn.State != System.Data.ConnectionState.Open)
                {
                    conn.Open();
                }
                return cmd.ExecuteNonQuery();
            }
        }
    }
}
